package com.badshun.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Touchpad;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badshun.player.MainPlayer;
import com.badshun.player.MainPlayer.Direction;
import com.badshun.player.MainPlayer.State;
import com.badshun.weapon.Wand;

public class GameUI {
	public Touchpad touchpad;
	public ImageButton aBtn;
	
	private int deadzoneRadius = 5;
	
	private Direction direction = Direction.DOWN;
	
	private MainPlayer player;
	
	private float knobPercentX = 0;
	private float knobPercentY = -1;
	
	public GameUI(MainPlayer player) {
		this.player = player;
		
		defineUI();
		
		setListener();
	}
	
	private void defineUI() {
		touchpad = new Touchpad(deadzoneRadius, new Touchpad.TouchpadStyle(new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("ui/background.png")))), new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("ui/knob.png"))))));
		aBtn = new ImageButton(new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("ui/a.jpg")))));
	}
	
	private void setListener() {
		aBtn.addListener(new InputListener() {
			@Override
			public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
				player.wand.createBall(knobPercentX, knobPercentY);
				
				return super.touchDown(event, x, y, pointer, button);
			}
		});
	}
	
	public void render() {
		if(touchpad.isTouched()) {
			knobPercentX = touchpad.getKnobPercentX();
			knobPercentY = touchpad.getKnobPercentY();
			
			if(knobPercentX > 0 && knobPercentY > 0) {
				direction = knobPercentX / knobPercentY > 1 ? Direction.RIGHT : Direction.UP;
			} else if(knobPercentX > 0 && knobPercentY < 0) {
				direction = knobPercentX / -knobPercentY > 1 ? Direction.RIGHT :  Direction.DOWN;
			} else if(knobPercentX < 0 && knobPercentY > 0) {
				direction = -knobPercentX / knobPercentY > 1 ? Direction.LEFT :  Direction.UP;
			} else if(knobPercentX < 0 && knobPercentY < 0) {
				direction = knobPercentX / knobPercentY > 1 ? Direction.LEFT :  Direction.DOWN;
			}
			
			if(direction == Direction.LEFT || direction == direction.RIGHT) {
				player.wand.mainWand.setRotation((float) Math.toDegrees(Math.atan(knobPercentY / knobPercentX)));
			} else if(direction == Direction.UP) {
				if(knobPercentX > 0) {
					player.wand.mainWand.setRotation((float) Math.toDegrees(Math.atan(knobPercentY / knobPercentX)));
				} else if(knobPercentX < 0) {
					player.wand.mainWand.setRotation(90 - (float) Math.toDegrees(Math.atan(knobPercentX / knobPercentY)));
				}
			} else if(direction == Direction.DOWN) {
				if(knobPercentX > 0) {
					player.wand.mainWand.setRotation(90 - (float) Math.toDegrees(Math.atan(knobPercentX / knobPercentY)));
				} else if(knobPercentX < 0) {
					player.wand.mainWand.setRotation((float) Math.toDegrees(Math.atan(knobPercentY / knobPercentX)));
				}
			}
			
			player.move(knobPercentX, knobPercentY, State.WALK , direction);
		} else {
			player.move(0, 0, State.IDLE, direction);
		}
	}
}

































